﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class LoginPage : UserControl
    {
        public event EventHandler<LoginEventArgs> OnLogin;

        private PageType afterLoginPage;

        public LoginPage(PageType afterLoginPage): this()
        {
            
            this.afterLoginPage = afterLoginPage;
        }

        public LoginPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string account = accountInput.Text;
            string password = passwordInput.Password;
            if (OnLogin != null)
            {
                OnLogin.Invoke(this, new LoginEventArgs(afterLoginPage));
            }

        }

        private void accountInput_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void passwordInput_PasswordChanged(object sender, RoutedEventArgs e)
        {
            updatePasswordHintVisibility();
        }


        private void updatePasswordHintVisibility()
        {
            passwordHint.Visibility = string.IsNullOrEmpty(passwordInput.Password) && !passwordInput.IsFocused ? Visibility.Visible : Visibility.Collapsed;
        }

        private void passwordInput_GotFocus(object sender, RoutedEventArgs e)
        {

            updatePasswordHintVisibility();
        }

        private void passwordInput_LostFocus(object sender, RoutedEventArgs e)
        {

            updatePasswordHintVisibility();
        }
    }


    public class LoginEventArgs : EventArgs
    {

        private PageType afterLoginPage;
        public LoginEventArgs(PageType afterLoginPage)
        {
            this.afterLoginPage = afterLoginPage;

        }

        public PageType AfterLoginPage 
        { 
            get 
            { 
                return afterLoginPage; 
            } 
        }


    }
}
